package com.iflytek.universitymanagement.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.domain.StudentCompetition;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.iflytek.universitymanagement.domain.StudentScore;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author louyp
 * @since 2024-11-26
 */
@Mapper
public interface StudentCompetitionMapper extends BaseMapper<StudentCompetition> {
   // 根据学生ID查询学生的竞赛记录
    @Select("SELECT * FROM student_competition WHERE student_id = #{studentId}")
    List<StudentCompetition> selectByStudentId(Integer studentId);

    @Select("SELECT sc.*, s.name, c.name AS competitionName " +
            "FROM student_competition sc " +
            "LEFT JOIN student s ON sc.student_id = s.student_id " +
            "LEFT JOIN competition c ON sc.competition_id = c.competition_id ${ew.customSqlSegment}")
    <T> IPage<StudentCompetition> selectByPage(Page<T> tPage, @Param("ew") QueryWrapper<StudentScore> queryWrapper);


    @Select("SELECT COUNT(*) FROM student_competition WHERE student_id = #{studentId}")
    int countParticipations(Integer studentId);

    @Select("SELECT COUNT(*) FROM student_competition WHERE student_id = #{studentId} AND awards IS NOT NULL")
    int countAwards(Integer studentId);

    @Select("SELECT COUNT(*) FROM student_competition WHERE student_id = #{studentId} AND participation_status = 'active'")
    int countActiveCompetitions(Integer  studentId);
}
